<?php


/**
* CakePHP Schema Plugin
* http://cake-schema-plugin.googlecode.com
*
* @package schema
* @subpackage schema.models.behaviors
* @author Ivan Chistyakov <zetruger@gmail.com>
* @version $Id: x_form.php 185 2010-11-15 09:11:24Z zetruger@gmail.com $
* @link $HeadURL: http://cake-schema-plugin.googlecode.com/svn/trunk/models/behaviors/x_form.php $
* @license MIT License (http://opensource.org/licenses/mit-license.php)
*/


class XFormBehavior extends ModelBehavior {

var $_formSchema = array(
	'image' => array('type' => 'image'),
	'flash' => array('type' => 'file')
);

var $_defaults = array(
	'formSchema' => array()
);

var $settings = array();

var $_cached = array();

function setup(&$Model, $settings = array()) {
	App::build(array('locales' => array(APP . 'plugins' . DS . 'schema' . DS . 'locale' . DS)));
	$this->settings[$Model->alias] = $settings;
	//extract($this->_getSettings($Model));
	//$Model->Behaviors->attach('Schema.Uploadable', $settings);
	$this->_cached = (array)Cache::read('x_form');
}

function _getSettings(&$Model, $settings = array()) {
	$settings = Set::merge($this->settings[$Model->alias], $settings);
	if (isset($settings['noUseModelVars']) && $settings['noUseModelVars']) {
		return Set::merge($this->_defaults, $settings);
	} else {
		return Set::merge($this->_defaults, array_intersect_key(get_object_vars($Model), $this->_defaults), $settings);
	}
}

//function getFieldType(&$Model, $fieldName) {
//}

function _domains(&$Model) {
	$domains = array();
	$searchDomains = array('x_form_' . Inflector::tableize($Model->alias), 'x_form_' . Inflector::tableize($Model->name), 'x_form_' . $Model->useTable, 'x_form_' . $Model->table, 'x_form', 'x_form_default');
	$searchPaths = App::path('locales');
	$language = Configure::read('Config.language');
	foreach ($searchDomains as $domain) {
		foreach ($searchPaths as $directory) {
			$filename = $directory . $language . DS . 'LC_MESSAGES' . DS . $domain;
			if ((is_file($filename . '.mo') || is_file($filename . '.po')) && !in_array($domain, $domains)) {
				$domains[] = $domain;
			}
		}
	}
	return $domains;
}

function _getText(&$Model, $msg) {
	foreach ($this->_domains($Model) as $domain) {
		foreach ((array)$msg as $msgIn) {
			$msgOut = __d($domain, $msgIn, TRUE);
			if ($msgOut != $msgIn) {
				return $msgOut;
			}
		}
	}
	if (is_scalar($msg)) {
		return $msg;
	}
	return FALSE;
}

function __dn(&$Model, $singular, $plural, $count) {
	foreach ($this->_domains($Model) as $domain) {
		foreach (array_combine((array)$singular, (array)$plural) as $singularMsgIn => $pluralMsgIn) {
			$msgOut = __dn($domain, $singularMsgIn, $pluralMsgIn, $count, TRUE);
			if (($msgOut != $singularMsgIn) && ($msgOut != $pluralMsgIn)) {
				return $msgOut;
			}
		}
	}
	if (is_scalar($singular) && is_scalar($plural)) {
		return ($count == 1) ? $singular : $plural;
	}
	return FALSE;
}

function getSingularLabel(&$Model) {
	$msgIn = array($Model->alias . '-singular', $Model->alias);
	$msgOut = $this->_getText($Model, $msgIn);
	if (($msgOut != $msgIn) && ($msgOut !== FALSE)) {
		return $msgOut;
	}
	if ((bool)preg_match('~^Attachment([a-z0-9]+)$~i', $Model->alias, $match)) {
		$msgIn = array($match[1] . '-singular', $match[1]);
		$msgOut = $this->_getText($Model, $msgIn);
		if (($msgOut != $msgIn) && ($msgOut !== FALSE)) {
			return $msgOut;
		}
	}
	return Inflector::humanize($Model->alias);
}

function getPluralLabel(&$Model) {
	$pluralAlias = Inflector::pluralize($Model->alias);
	$msgIn = array($pluralAlias . '-plural', $pluralAlias);
	$msgOut = $this->_getText($Model, $msgIn);
	if (($msgOut != $msgIn) && ($msgOut !== FALSE)) {
		return $msgOut;
	}
	if ((bool)preg_match('~^Attachment([a-z0-9]+)$~i', $pluralAlias, $match)) {
		$msgIn = array($match[1] . '-plural', $match[1]);
		$msgOut = $this->_getText($Model, $msgIn);
		if (($msgOut != $msgIn) && ($msgOut !== FALSE)) {
			return $msgOut;
		}
	}
	return Inflector::humanize($pluralAlias);
}

function getNumericLabel(&$Model, $count = 1) {
	$pluralAlias = Inflector::pluralize($Model->alias);
	$singularMsgIn = array($Model->alias . '-singular', $Model->alias);
	$pluralMsgIn = array($pluralAlias . '-plural', $pluralAlias);
	$msgOut = $this->__dn($Model, $singularMsgIn, $pluralMsgIn, $count);
	if (($msgOut != $singularMsgIn) && ($msgOut != $pluralMsgIn) && ($msgOut !== FALSE)) {
		return $msgOut;
	}
	if ((bool)preg_match('~^Attachment([a-z0-9]+)$~i', $Model->alias, $singularMatch) && (bool)preg_match('~^Attachment([a-z0-9]+)$~i', $pluralAlias, $pluralMatch)) {
		$singularMsgIn = array($singularMatch[1] . '-singular', $singularMatch[1]);
		$pluralMsgIn = array($pluralMatch[1] . '-plural', $pluralMatch[1]);
		$msgOut = $this->__dn($Model, $singularMsgIn, $pluralMsgIn, $count);
		if (($msgOut != $singularMsgIn) && ($msgOut != $pluralMsgIn) && ($msgOut !== FALSE)) {
			return $msgOut;
		}
	}
	return Inflector::humanize(($count == 1) ? $Model->alias : $pluralAlias);
}


function _getTextByFieldName(&$Model, $fieldName) {
	$msgIn = array($Model->alias . '.' . $fieldName, 'Model.' . $fieldName, '*.' . $fieldName);
	$msgOut = $this->_getText($Model, $msgIn);
	if (($msgOut != $msgIn) && ($msgOut !== FALSE)) {
		return $msgOut;
	}
	return FALSE;
}

function _getFieldLabel(&$Model, $fieldName) {
	$fieldLabel = $this->_getTextByFieldName($Model, $fieldName);
	if (($fieldLabel != $fieldName) && ($fieldLabel !== FALSE)) {
		return $fieldLabel;
	}
	$languages = implode('|', (array)Configure::read('Config.languages'));
	if (!empty($languages)) {
		if ((bool)preg_match('~^(' . $languages . ')_([a-z0-9_]+)_([0-9]+)$~i', $fieldName, $match)) {
			$fieldLabel = $this->_getTextByFieldName($Model, $match[2]);
			if (($fieldLabel != $match[2]) && ($fieldLabel !== FALSE)) {
				return strtoupper($match[1]) . ' ' . $fieldLabel . ' ' . $match[3];
			}
		}
		if ((bool)preg_match('~^(' . $languages . ')_([a-z0-9_]+)$~i', $fieldName, $match)) {
			$fieldLabel = $this->_getTextByFieldName($Model, $match[2]);
			if (($fieldLabel != $match[2]) && ($fieldLabel !== FALSE)) {
				return strtoupper($match[1]) . ' ' . $fieldLabel;
			}
		}
	}
	if ((bool)preg_match('~^([a-z0-9_]+)_([0-9]+)$~i', $fieldName, $match)) {
		$fieldLabel = $this->_getTextByFieldName($Model, $match[1]);
		if (($fieldLabel != $match[1]) && ($fieldLabel !== FALSE)) {
			return $fieldLabel . ' ' . $match[2];
		}
	}
	if ($Model->isForeignKey($fieldName)) {
		foreach ($Model->belongsTo as $assocAlias => $assocData) {
			list($plugin, $assocAlias) = pluginSplit($assocAlias);
			if ($fieldName == $assocData['foreignKey']) {
				if (ClassRegistry::isKeySet($assocAlias)) {
					$object = &ClassRegistry::getObject($assocAlias);
					if (is_a($object, 'Model') && $object->Behaviors->attached('XForm')) {
						return $object->getSingularLabel();
					}
				} elseif (isset($Model->{$assocAlias})) {
					$object = &$Model->{$assocAlias};
					if (is_a($object, 'Model') && $object->Behaviors->attached('XForm')) {
						return $object->getSingularLabel();
					}
				}
				if (ClassRegistry::isKeySet($assocData['className'])) {
					$object = &ClassRegistry::getObject($assocData['className']);
					if (is_a($object, 'Model') && $object->Behaviors->attached('XForm')) {
						return $object->getSingularLabel();
					}
				} elseif (isset($Model->{$assocData['className']})) {
					$object = &$Model->{$assocData['className']};
					if (is_a($object, 'Model') && $object->Behaviors->attached('XForm')) {
						return $object->getSingularLabel();
					}
				}
				return FALSE;
			}
		}
	}
	foreach ($Model->hasAndBelongsToMany as $assocAlias => $assocData) {
		list($plugin, $assocAlias) = pluginSplit($assocAlias);
		if (($fieldName == $assocAlias) || ($fieldName == $assocData['className'])) {
			if ($fieldName == $assocAlias) {
				if (ClassRegistry::isKeySet($assocAlias)) {
					$object = &ClassRegistry::getObject($assocAlias);
					if (is_a($object, 'Model') && $object->Behaviors->attached('XForm')) {
						return $object->getPluralLabel();
					}
				} elseif (isset($Model->{$assocAlias})) {
					$object = &$Model->{$assocAlias};
					if (is_a($object, 'Model') && $object->Behaviors->attached('XForm')) {
						return $object->getPluralLabel();
					}
				}
			}
			if ($fieldName == $assocData['className']) {
				if (ClassRegistry::isKeySet($assocData['className'])) {
					$object = &ClassRegistry::getObject($assocData['className']);
					if (is_a($object, 'Model') && $object->Behaviors->attached('XForm')) {
						return $object->getPluralLabel();
					}
				} elseif (isset($Model->{$assocData['className']})) {
					$object = &$Model->{$assocData['className']};
					if (is_a($object, 'Model') && $object->Behaviors->attached('XForm')) {
						return $object->getPluralLabel();
					}
				}
			}
			return FALSE;
		}
	}
	if (ClassRegistry::isKeySet($fieldName)) {
		$object = &ClassRegistry::getObject($fieldName);
		if (is_a($object, 'Model') && $object->Behaviors->attached('XForm')) {
			return $object->getSingularLabel();
		}
	} elseif (isset($Model->{$fieldName})) {
		$object = &$Model->{$fieldName};
		if (is_a($object, 'Model') && $object->Behaviors->attached('XForm')) {
			return $object->getSingularLabel();
		}
	}
	return Inflector::humanize($fieldName);
}

function getFieldLabel(&$Model, $fieldName) {
	$language = Configure::read('Config.language');
	$key = $Model->alias . '.' . $fieldName;
	if (isset($this->_cached[$language][$key]) && !empty($this->_cached[$language][$key])) {
		return $this->_cached[$language][$key];
	}
	$this->_cached[$language][$key] = $this->_getFieldLabel($Model, $fieldName);
	if (isset($this->_cached[$language][$key]) && !empty($this->_cached[$language][$key])) {
		Cache::write('x_form', $this->_cached);
		return $this->_cached[$language][$key];
	}
	return Inflector::humanize($fieldName);
}

function getInputOptions(&$Model, $fieldName) {
return array();
}

}


?>